<template>
  <div>
    <headerBox></headerBox>
    <div class="grid-container">
      <div class="left-item">
        <div class="chart-item">
          <h3 class="echart-title">
            进出站累计客流
          </h3>
          <enterAndExit :echartData="echartData"></enterAndExit>
        </div>
        <div class="chart-item  ">
          <h3 class="echart-title">
            区域客流密度
            <span class="right-text">更新时间{{ currentTime }}</span>
          </h3>
          <passengerModel :regionData="regionData"></passengerModel>
        </div>
        <div class="chart-item ">
          <h3 class="echart-title">
            站内实时客流
            <span class="right-text">更新时间{{ currentTime }}</span>
          </h3>
          <echartModel :waitData="stationData"></echartModel>
        </div>
        <div class="chart-item ">
          <h3 class="echart-title">
            候车区实时客流
            <span class="right-text">更新时间{{ currentTime }}</span>
          </h3>
          <echartModel :waitData="waitData"></echartModel>
        </div>
      </div>
      <div class="right-item">
        <div class="grid-item aa ">
          <h3 class="echart-title">
            站内图
            <span class="map-tips-container">
              <span class="map-tips tips-1"></span>
              <span>客流分布</span>
              <span class="map-tips tips-2"></span>
              <span>设备位置</span>
            </span>
          </h3>
          <img class="map-img" src="../assets/station-1.png" alt="" />
          <img class="map-bottom" src="../assets/station-2.png" alt="" />
        </div>
        <div class="grid-item bb ">
          <h3 class="echart-title">
            异常设备
          </h3>
          <div class="deviceAlert">
            {{ deviceErrorList.length }}
          </div>
        </div>
        <div class="grid-item cc">
          <deviceListBox :deviceErrorList="deviceErrorList"></deviceListBox>
        </div>
        <div class="grid-item dd">
          <broadcast :weatherInfo="weatherInfo"></broadcast>
        </div>
        <div class="grid-item ee">
          <h3 class="echart-title">
            车厢及候车区拥挤度
            <span class="float-right-text">
              <span class="line-icon">3</span>
              <span>双龙方向</span>
              <span class="time-icon">{{ trainInfo.up1 }}分钟</span>
              <span>福保方向</span>
              <span class="time-icon">{{ trainInfo.down1 }}分钟</span>
              <span class="line-icon line14">14</span>
              <span>肿瘤医院方向</span>
              <span class="time-icon">{{ trainInfo.up2 }}分钟</span>
              <span>大运方向</span>
              <span class="time-icon">{{ trainInfo.down2 }}分钟</span>
            </span>
          </h3>
          <div class="train-container">
            <train-box :trainObj="trainObjUp"></train-box>
            <train-box :trainObj="trainObjDown"></train-box>
            <ul class="train-status">
              <li>
                <span class="status-1">拥挤</span>
                <div class="status-box">
                  <img src="../assets/status-3.png" alt="" />
                </div>

                <p>拥挤</p>
              </li>
              <li>
                <span class="status-2">适中</span>
                <div class="status-box">
                  <img src="../assets/status-2.png" alt="" />
                </div>
                <p>适中</p>
              </li>
              <li>
                <span class="status-3">稀疏</span>
                <div class="status-box">
                  <img src="../assets/status-1.png" alt="" />
                </div>
                <p>稀疏</p>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import headerBox from '../components/system-header';
import echartModel from '../components/echartModel';
import passengerModel from '../components/passenger';
import enterAndExit from '../components/enterAndExit';
import deviceListBox from '../components/devicesList';
import broadcast from '../components/broadcast';
import trainBox from '../components/trainBox';
import {
  customerFlow,
  regionFlow,
  waitInfo,
  deviceError,
  stationFlow,
  transferFlow,
  trainInfo,
  baseInfo
} from '../service/user';
export default {
  components: {
    headerBox,
    echartModel,
    passengerModel,
    enterAndExit,
    deviceListBox,
    broadcast,
    trainBox
  },
  data() {
    return {
      echartData: {
        xAxis: [],
        series1: [],
        series2: []
      },
      // 区域客流密度
      regionData: {
        xAxis: [],
        series1: []
      },
      //候车区实时客流
      waitData: {
        xAxis: [],
        series1: []
      },
      stationData: {
        xAxis: [],
        series1: []
      },
      currentTime: '',
      deviceErrorList: [],
      weatherInfo: {},
      trainInfo: {
        up1: '',
        down1: '',
        up2: '',
        down2: ''
      },
      trainObjUp: {
        title: '肿瘤医院方向',
        carriage_1: {},
        carriage_2: {},
        carriage_3: {}
      },
      trainObjDown: {
        title: '大运方向',
        carriage_1: {},
        carriage_2: {},
        carriage_3: {}
      },
      timer: ''
    };
  },
  mounted() {
    var dayjs = require('dayjs');
    this.getCustomerFlow();
    this.getBaseInfo();
    this.getTrainInfo();
    this.currentTime = dayjs().format('HH:mm:ss');
    clearInterval(this.timer);
    // 定时任务
    this.timer = setInterval(() => {
      this.getCustomerFlow();
      this.getBaseInfo();
      this.getTrainInfo();
      this.currentTime = dayjs().format('HH:mm:ss');
    }, 30000);
    // 当屏幕发生变化是 重新渲染图表
  },
  methods: {
    // 天气情况
    getBaseInfo() {
      baseInfo(1, 1, 1).then(res => {
        this.weatherInfo = res.data.result[0].weather_info;
      });
    },
    // 获取列车到站信息
    getTrainInfo() {
      // 上行
      trainInfo(1, 1, 1).then(res => {
        this.trainInfo.up1 = res.data.result[0].tarin_state.arrival_time;
        this.trainInfo.down1 = res.data.result[1].tarin_state.arrival_time;
        this.trainObjUp.carriage_1 =
          res.data.result[0].carriage_state.carriage_1;
        this.trainObjUp.carriage_2 =
          res.data.result[0].carriage_state.carriage_2;
        this.trainObjUp.carriage_3 =
          res.data.result[0].carriage_state.carriage_3;
      });
      // 下行
      trainInfo(1, 0, 1).then(res => {
        this.trainInfo.up2 = res.data.result[0].tarin_state.arrival_time;
        this.trainInfo.down2 = res.data.result[1].tarin_state.arrival_time;
        this.trainObjDown.carriage_1 =
          res.data.result[0].carriage_state.carriage_1;
        this.trainObjDown.carriage_2 =
          res.data.result[0].carriage_state.carriage_2;
        this.trainObjDown.carriage_3 =
          res.data.result[0].carriage_state.carriage_3;
      });
    },
    // 候车区域和车辆拥挤度
    //
    // 获取区域客流密度
    getTransferFlow() {
      transferFlow('2020-01-01 00:00:00', '2020-01-01 08:00:00').then();
    },
    getCustomerFlow() {
      customerFlow('2020-01-01 00:00:00', '2020-01-01 08:00:00', 60).then(
        res => {
          if (res.data.code === 200) {
            this.echartData.xAxis = [];
            this.echartData.series1 = [];
            this.echartData.series2 = [];
            res.data.result.passenger_flow.forEach(item => {
              let returnTime = item.start;
              let dayjs = require('dayjs');
              returnTime = dayjs(returnTime).format('HH');

              this.echartData.xAxis.push(returnTime);
              this.echartData.series1.push(item.flow_in);
              this.echartData.series2.push(item.flow_out);
            });
          }
        }
      );
      regionFlow().then(res => {
        if (res.data.code === 200) {
          this.regionData.xAxis = [];
          this.regionData.series1 = [];

          res.data.result.area_density.forEach(item => {
            this.regionData.xAxis.push(item.area_id);
            this.regionData.series1.push(item.flow_number);
          });
        }
      });
      waitInfo().then(res => {
        if (res.data.code === 200) {
          this.waitData.xAxis = [];
          this.waitData.series1 = [];
          res.data.result.wait_areas.forEach(item => {
            let returnTime = item.start;
            let dayjs = require('dayjs');
            returnTime = dayjs(returnTime).format('HH');
            this.waitData.xAxis.push(returnTime);
            let num = 0;
            num = item.id_1 + item.id_2 + item.id_3;
            this.waitData.series1.push(num);
          });
        }
      });

      stationFlow().then(res => {
        if (res.data.code === 200) {
          this.stationData.xAxis = [];
          this.stationData.series1 = [];
          res.data.result.station_flow.forEach(item => {
            let returnTime = item.start;
            let dayjs = require('dayjs');
            returnTime = dayjs(returnTime).format('HH');
            this.stationData.xAxis.push(returnTime);
            this.stationData.series1.push(item.flow_number);
          });
        }
      });
      // 故障设备
      deviceError().then(res => {
        this.deviceErrorList = res.data.result;
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.echart-title {
  padding: 0 16px;
  .right-text {
    float: right;
  }
}

.grid-container {
  display: grid;
  grid-template-columns: 24% 1fr;
  grid-template-rows: 1fr;
  grid-column-gap: 10px;
  grid-row-gap: 10px;
  height: calc(100vh - 90px);
  margin-top: 10px;
  padding: 10px 10px 0;

  .grid-item {
    font-size: 20px;
    background: #121626;
  }
  .map-img {
    height: calc(100vh - 520px);
    width: 90%;
    margin: 20px auto 0;
  }
  .map-bottom {
    width: 80%;
    position: absolute;
    bottom: 0;
    left: 10%;
  }
  .car-img {
    width: 90%;
    margin: 30px auto 0;
    height: 120px;
  }
  .left-item {
    // display: grid;
    // grid-template-columns: 1fr;
    // grid-template-rows: repeat(4 1fr);
    // grid-column-gap: 10px;
    // grid-row-gap: 10px;
    height: calc(100vh - 90px);
    .chart-item {
      font-size: 20px;
      background: #121626;
      height: calc(25vh - 30px);
      overflow: hidden;
      margin-bottom: 10px;
      width: 100%;
    }
  }
  .right-item {
    display: grid;
    grid-template-columns: 4fr 2fr;
    grid-template-rows: 200px 3fr 44px 240px;
    grid-column-gap: 10px;
    grid-row-gap: 10px;
    height: calc(100vh - 90px);
    grid-template-areas:
      'aa bb'
      'aa cc'
      'dd dd'
      'ee ee';
  }
  .aa {
    grid-area: aa;
    position: relative;
  }
  .bb {
    grid-area: bb;
  }
  .cc {
    grid-area: cc;
    overflow: hidden;
  }
  .dd {
    grid-area: dd;
  }
  .ee {
    grid-area: ee;
  }

  .deviceAlert {
    background: url('../assets/device-number.png') no-repeat 8px 0px;
    width: 300px;
    height: 120px;
    font-size: 50px;
    line-height: 120px;
    margin: 20px auto 0;
  }
  .wordReport {
    text-align: left;
    line-height: 44px;
    padding: 0 20px;
    span {
      margin-right: 240px;
      color: #eee;
      font-size: 16px;
    }
  }
  .float-right-text {
    float: right;
  }
  .line-icon {
    display: inline-block;
    width: 24px;
    height: 24px;
    background: #00a4df;
    line-height: 24px;
    color: #fff !important;
    border: 1px solid #fff;
    border-radius: 6px;
    text-align: center;
    margin-right: 10px;
  }
  .line14 {
    background: #8495aa;
    margin-left: 40px;
  }
  .time-icon {
    color: #fff !important;
    padding: 4px 10px;
    border: 1px solid #fff;
    border-radius: 4px;
    margin-left: 10px;
    margin-right: 10px;
  }
  .map-tips-container {
    float: right;
    margin-right: 20px;
  }
  .map-tips {
    width: 8px;
    height: 8px;
    display: inline-block;
    border-radius: 50%;
    margin: 0 4px 0 30px;
  }
  .tips-1 {
    background: #656565;
    border: 2px solid #979797;
  }
  .tips-2 {
    background: #ffffff;
    border: 2px solid #323fff;
  }
  .train-container {
    text-align: left;
    padding-top: 10px;
  }
  .train-status {
    display: inline-block;
    width: 300px;
    vertical-align: top;
    margin-top: 50px;
    margin-left: 20px;
    li {
      display: inline-block;
      width: 100px;
      text-align: center;
      vertical-align: top;
      span {
        font-size: 14px;
        color: #999;
        position: relative;
        margin-left: 16px;
      }
      span:before {
        display: inline-block;
        content: '';
        width: 14px;
        height: 14px;
        background: #ddd;
        position: absolute;
        left: -18px;
        top: 0;
        border-radius: 2px;
      }
      .status-1:before {
        background: #de6173;
      }
      .status-2:before {
        background: #ec924b;
      }
      .status-3:before {
        background: #61daa2;
      }
      .status-box {
        height: 32px;
        padding-top: 10px;
      }
      img {
        display: block;
        margin: 0 auto;
      }
      p {
        color: #999;
        padding: 0;
        margin: 0;
        font-size: 14px;
      }
    }
  }
}
</style>
